home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / except.zip / MAIN.CPP < prev    next >
C/C++ Source or Header  |  1993-03-02  |  5KB  |  173 lines

  1. /******************************************************************************\
  2. *
  3. *  PROGRAM:     MAIN.C
  4. *
  5. *  PURPOSE:     To test the resource-only DLL "B4DMSG.DLL".
  6. *                This is an extension of the resdll sample in
  7. *                the October pre-release.  I have added Message
  8. *                Compiler and Exception Handling.
  9. *
  10. *  FUNCTIONS:   WinMain()     - initialization, create window, msg loop
  11. *               MainWndProc() - processes main window msgs
  12. *
  13. *  COMMENTS:    
  14. *
  15. *
  16. \******************************************************************************/
  17.  
  18. #include <windows.h>
  19. #include "Main.h"
  20. #include "b4dmsgrs.h"
  21.  
  22. static B4DMsgResource *pMessageResource=0;
  23.  
  24. static void CauseException();
  25. static int MainExceptionFilter
  26.     ( 
  27.     DWORD                    dwExceptionCode,
  28.     LPEXCEPTION_POINTERS    pExceptionPointers
  29.     );
  30.  
  31. /******************************************************************************\
  32. *
  33. *  FUNCTION:    WinMain (standard WinMain INPUTS/RETURNS)
  34. *
  35. *  GLOBAL VARS: ghLib - library instance handle
  36. *
  37. *  LOCAL VARS:  hwnd - handle of the main standard window
  38. *               msg  - msg to get/dispatch
  39. *
  40. \******************************************************************************/
  41.  
  42. int WINAPI WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  43.                     LPSTR lpCmdLine,  int nCmdShow)
  44. {
  45.     HWND hwnd;
  46.       MSG msg;
  47.  
  48.     if ( (pMessageResource = new B4DMsgResource) == 0 )
  49.         {
  50.         MessageBox( NULL, "Unable to load resource DLL", __FILE__, MB_ICONSTOP | MB_OK );
  51.         return FALSE;
  52.         }
  53.  
  54. try
  55.     {
  56.     if (!hPrevInstance)
  57.           {
  58.         WNDCLASS wc;
  59.  
  60.         wc.style         = CS_HREDRAW | CS_VREDRAW;;
  61.         wc.lpfnWndProc   = (WNDPROC)MainWndProc;
  62.         wc.cbClsExtra    = 0;
  63.         wc.cbWndExtra    = 0;
  64.         wc.hInstance     = hInstance;
  65.         wc.hIcon         = NULL;
  66.         wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  67.         wc.hbrBackground = GetStockObject (WHITE_BRUSH);
  68.         wc.lpszMenuName  = NULL;
  69.         wc.lpszClassName = (LPCTSTR) "B4DMSG";
  70.  
  71.         if (!RegisterClass (&wc))
  72.             {
  73.               MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed",
  74.                   (LPCTSTR) "Err! - MAIN", MB_OK | MB_ICONEXCLAMATION);
  75.               return(FALSE);
  76.             }
  77.           }
  78.  
  79.     if (!(hwnd = CreateWindow ("B4DMSG", "B4DMSG DLL Sample Application",
  80.                              WS_OVERLAPPEDWINDOW,
  81.                              CW_USEDEFAULT, CW_USEDEFAULT,
  82.                              CW_USEDEFAULT, CW_USEDEFAULT,
  83.                              NULL, NULL, hInstance, NULL)))
  84.     return (NULL);
  85.  
  86.   ShowWindow (hwnd, nCmdShow);
  87.  
  88.   while (GetMessage (&msg, NULL, NULL, NULL))
  89.   {
  90.     TranslateMessage (&msg);
  91.     DispatchMessage  (&msg);
  92.   }
  93.  
  94.     return msg.wParam;
  95. }
  96. except( MainExceptionFilter( GetExceptionCode(), GetExceptionInformation() ) )
  97. {
  98. } // except
  99.  
  100. }
  101.  
  102. /******************************************************************************\
  103. *
  104. *  FUNCTION:    MainWndProc (standard window procedure INPUTS/RETURNS)
  105. *
  106. *  GLOBAL VARS: ghLib - library instance handle
  107. *
  108. *  LOCAL VARS:  hbm - handle of bitmap in THE_DLL.DLL
  109. *
  110. \******************************************************************************/
  111.  
  112. LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam,
  113.                               LPARAM lParam)
  114. {
  115.     switch (message)
  116.           {
  117.         case WM_CHAR:
  118.             CauseException();
  119.               return 0;
  120.         case WM_DESTROY:
  121.             PostQuitMessage (NULL);
  122.               break;
  123.         default:
  124.               return (DefWindowProc(hwnd, message, wParam, lParam));
  125.           }
  126.  
  127.     return NULL;
  128. }
  129.  
  130. static void CauseException()
  131. {
  132.     static const int ARG_COUNT = 2;
  133.     DWORD exceptionArguments[2];
  134.  
  135.     exceptionArguments[0] = (DWORD) "aaaa";
  136.     exceptionArguments[1] = 123;
  137.  
  138.     RaiseException
  139.         (
  140.         B4D_ERROR_DEMO,
  141.         0, // not EXCEPTION_NONCONTINUABLE,
  142.         ARG_COUNT,
  143.         exceptionArguments            
  144.         );
  145. } // static void CauseException()
  146.  
  147. static int MainExceptionFilter
  148.     ( 
  149.     DWORD                    dwExceptionCode,
  150.     LPEXCEPTION_POINTERS    pExceptionPointers
  151.     )
  152. {
  153.     int nResult = EXCEPTION_CONTINUE_SEARCH;
  154.  
  155.     // see if it's one of ours
  156.     // if it's a B4D Exception, display a message
  157.     if ( HIWORD(dwExceptionCode & 0x0FFF0000) == B4D_FACILITY )
  158.         {
  159.         DWORD *pArgs = pExceptionPointers->ExceptionRecord->ExceptionInformation;
  160.         MessageBox
  161.             (
  162.             NULL,
  163.             pMessageResource->get( dwExceptionCode, pArgs ),
  164.             "Error!",
  165.             MB_ICONSTOP | MB_OK
  166.             );
  167.         nResult = EXCEPTION_EXECUTE_HANDLER;
  168.         } // if ( HIWORD(dwExceptionCode & 0x0FFF0000) == B4D_FACILITY )
  169.  
  170.     return nResult;
  171.     
  172. } // static int MainExceptionFilter
  173.